package com.zhongge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhongge.entity.Dest;
import com.zhongge.entity.Holiday;
import com.zhongge.service.impl.DestServiceImpl;
import com.zhongge.vo.DestInfoVO;
import com.zhongge.vo.HistoryDestInfoVO;
import com.zhongge.vo.HolidayVO;
import com.zhongge.vo.StuDestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Controller
@RequestMapping("/dest")
public class DestController {
    @Autowired
    private DestServiceImpl destService;
    //根据假期id查询出所有的学生去向
    @PostMapping("/list")
    @ResponseBody
    public List<DestInfoVO> getDestInfoByHolidayId(Integer holidayId){
        return destService.getDestInfoByHolidayId(holidayId);
    }

    //跳转到学生登记假期页面
    @GetMapping("/dispatchStudentHoliday")
    public String dispatchStudentHoliday(){
        return "/studestsys/student/student-holiday";
    }

    //跳转到历史记录页面
    @GetMapping("/dispatchStudentHistory")
    public String dispatchStudentHistory(){
        return "/studestsys/student/student-history";
    }

    //跳转到学生个人信息页面
    @GetMapping("/dispatchStudentInfo")
    public String dispatchStudentInfo(){
        return "/studestsys/student/student-info";
    }

    //查询假期表：只是将所有状态为1的假期查询出
    @GetMapping("/holiday/list")
    @ResponseBody
    public Map<String, Object> getHoliday(Integer page, Integer rows){
        Map<String, Object> map = new HashMap<>();
        IPage<StuDestVO> holidayOfSTatu = destService.getHolidayOfSTatu(page, rows);
        map.put("rows",holidayOfSTatu.getRecords());
        map.put("total",holidayOfSTatu.getTotal());
        return map;
    }

    //假期去向登记
    @PostMapping("/registration/saveOrUpdate")
    @ResponseBody
    public Map<String, Object> registration(Dest dest){
        Map<String, Object> map = new HashMap<>();
        try{
            boolean save = destService.save(dest);
            if (save){
                map.put("success",true);
                map.put("message","提交成功！");
            }else {
                map.put("success",false);
                map.put("message","提交失败!!请重试");
            }
        }catch (DataIntegrityViolationException e){
            // 捕获数据库约束异常（唯一键、外键等都属于此类）
            map.put("success",false);
            map.put("message","你已经登记过了，不可重复登记");
        }catch (Exception e){
            map.put("success",false);
            map.put("message","出现异常！请联系管理员"+e.getMessage());
        }
        return map;
    }

    //查询学生登记信息
    @GetMapping("/registration/history")
    @ResponseBody
    public Map<String, Object> getHistoryDestInfo(Integer stuId,Integer page, Integer rows){
        Map<String, Object> map = new HashMap<>();
        IPage<HistoryDestInfoVO> historyDestInfo = destService.getHistoryDestInfo(page, rows, stuId);
        map.put("rows",historyDestInfo.getRecords());
        map.put("total",historyDestInfo.getTotal());
        return map;
    }
}
